package view.client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Updater {
    private final static String versionURL = "cp/version.html";
    private final static String historyURL = "cp/history.html";
    public static String getLatestVersion()
    {
        FileReader reader;
        try {
            reader = new FileReader(new File("ip.txt"));
            BufferedReader leitor = new BufferedReader(reader);
            String linha = null;  
            linha = leitor.readLine();
            reader.close();
            String data = getData(linha+versionURL);
            return data.substring(data.indexOf("[version]")+9,data.indexOf("[/version]"));
        }  catch (IOException ex) {
            Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Updater.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public static String getWhatsNew() throws Exception
    {
        FileReader reader;
        reader = new FileReader(new File("ip.txt"));
        BufferedReader leitor = new BufferedReader(reader);
        String linha = null;  
        linha = leitor.readLine();
        reader.close();
        String data = getData(linha+historyURL);
        return data.substring(data.indexOf("[history]")+9,data.indexOf("[/history]"));
    }
    public static String getData(String address)throws Exception
    {
        URL url = new URL(address);
        
        InputStream html = null;

        html = url.openStream();
        
        int c = 0;
        StringBuffer buffer = new StringBuffer("");

        while(c != -1) {
            c = html.read();
            
        buffer.append((char)c);
        }
        return buffer.toString();
    }
}
